import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import index from '../views/index.vue'
import login from '../views/Login'
import register from '../views/register'
import basicInfo from '../views/person/basicInfo'
import personIndex from '../views/person/index'
import myOrder from '../views/person/myOrder'
Vue.use(VueRouter)
const routes = [{ path: '/',name: 'Home',component: Home, redirect:"/index",
                     children:[{path: '/index',name:'index',component:index},
                              {path:'/person',component:personIndex,
							      children:[{path:'/basicInfo',name:basicInfo,component:basicInfo},
                                            {path:'/myOrder',name:'myOrder',component:myOrder}
                                 ]}
                             ]
                 },
                 {path: '/login',name: 'login',component: login},
                 {path:'/register', component:register},
               ]


const router = new VueRouter({
  routes,
  //每次路由跳转时滚动条回到最上方
  scrollBehavior(to, from,savedPosition) {
    return {
      x: 0,
      y: 0
    }
  }
})

router.beforeEach((to,from,next)=>{
  if(to.path==='/login'){
    return next()
  }
  if(to.path==='/register'){
	  return next()
  }
  let id=window.sessionStorage.getItem("id")
  if(id!==null){
    return next()
  }
  next('/login')
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}
export default router
